import { message } from 'antd';
import { addTrade } from '@/services/trade/trade';
import { getItem } from '@/services/item/item';

const ItemModel = {
  namespace: 'item',
  state: {
    item: undefined,
    status: false,
    responsive: false,
  },
  effects: {
    *getItem({ payload }, { call, put }) {
      const response = yield call(getItem, payload);
      yield put({
        type: 'saveItem',
        payload: response.data,
      });
      yield put({
        type: 'saveStatus',
        payload: true,
      });
    },
    *addTrade({ payload }, { call, put }) {
      const response = yield call(addTrade, payload);
      if (response.success) {
        message.success('创建成功');
        yield put({
          type: 'saveStatus',
          payload: false,
        });
      } else {
        message.error(`创建失败${response.message}`);
      }
    },
  },
  reducers: {
    saveItem(state, action) {
      return { ...state, item: action.payload || {} };
    },
    saveStatus(state, action) {
      return { ...state, status: action.payload };
    },
    changeResponsive(state, action) {
      return { ...state, responsive: action.payload };
    },
  },
};

export default ItemModel;
